<?php
class Pageley_Query_Condition_Factory {
	private static $instance;
    
    private function __construct() { }
    
    public static function getInstance() {
		if(self::$instance === null) {
			self::$instance = new Pageley_Query_Condition_Factory();
		}
		return self::$instance;
	}
    
    public function createCondition($type, $propertyName, $operator, $value, $not = false) {
    	$attribute = $type->getAttribute($propertyName);
    	$factory = Pageley_Serializer_Converter_Factory::getInstance();
    	$converter = $factory->getConverter($attribute);
    	$value = $converter->convertFromFormat($value);
        $condition = null;
        if($operator == "=") {
            $condition = new Pageley_Query_Condition_Equals($propertyName, $value);
        } else if($operator == "!=") {
            $condition = new Pageley_Query_Condition_NotEquals($propertyName, $value);
        } else if($operator == "<") {
            $condition = new Pageley_Query_Condition_LessThan($propertyName, $value);
        } else if($operator == ">") {
            $condition = new Pageley_Query_Condition_GreaterThan($propertyName, $value);
        } else if($operator == "<=") {
            $condition = new Pageley_Query_Condition_LessThanOrEquals($propertyName, $value);
        } else if($operator == ">=") {
            $condition = new Pageley_Query_Condition_GreaterThanEquals($propertyName, $value);
        } else if($operator == "[IN]") {
            $condition = new Pageley_Query_Condition_In($propertyName, $value);
        } else if($operator == "[LIKE]") {
            $condition = new Pageley_Query_Condition_Contains($propertyName, $value);
        } else {
            throw new Pageley_Mapper_Exception("Invalid operator '" . $operator . "' for condition.");
        }
        
        if($not) {
            $condition = new Pageley_Query_Condition_Not($condition);
        }
        return $condition;
        
    }
}